package com.junzhiit.nengji.app.web.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.bainaeco.framework.util.AssertUtil;
import com.junzhiit.nengji.app.service.NoticeService;
import com.junzhiit.nengji.app.web.vo.DataTablesParamVO;
import com.junzhiit.nengji.app.web.vo.NoticeAddVO;
import com.junzhiit.nengji.app.web.vo.NoticeEditVO;
import com.junzhiit.nengji.app.web.vo.NoticeReadVO;
import com.junzhiit.nengji.app.web.vo.ResultVO;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/notice")
public class NoticeController extends BaseController {

	@Autowired
	private NoticeService noticeService;

	@ApiOperation(value = "获取所有公告列表")
	@RequestMapping(method = RequestMethod.GET)
	public Map<String, Object> findAll(String sessionId, DataTablesParamVO dtp) {
		AssertUtil.notBlank(sessionId, "sessionId is required.");
		return noticeService.findAll(sessionId, dtp);
	}
	
	@ApiOperation(value = "获取所有公告消息列表")
	@RequestMapping(value = "/news",method = RequestMethod.GET)
	public Map<String, Object> findAllNoticeNews(DataTablesParamVO dtp) {
		return noticeService.findAllNoticeNews(dtp);
	}

	@ApiOperation(value = "根据ID查询公告")
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public ResultVO findById(@PathVariable String id) {
		AssertUtil.notBlank(id, "id is required.");
		return noticeService.findById(id);
	}

	@ApiOperation(value = "创建公告")
	@RequestMapping(method = RequestMethod.POST)
	public ResultVO add(String sessionId, NoticeAddVO noticeAddVO) {
		AssertUtil.notBlank(sessionId, "sessionId is required.");
		AssertUtil.notBlank(noticeAddVO.getName(), "name is required.");
		AssertUtil.notBlank(noticeAddVO.getContent(), "content is required.");
		return noticeService.add(sessionId, noticeAddVO);
	}

	@ApiOperation(value = "修改公告")
	@RequestMapping(method = RequestMethod.PUT)
	public ResultVO update(NoticeEditVO noticeEditVO) {
		AssertUtil.notBlank(noticeEditVO.getName(), "name is required.");
		AssertUtil.notBlank(noticeEditVO.getContent(), "content is required.");
		return noticeService.edit(noticeEditVO);
	}

	@ApiOperation(value = "删除公告")
	@RequestMapping(value = "/del", method = RequestMethod.PUT)
	public ResultVO delete(String id) {
		AssertUtil.notBlank(id, "id is required.");
		return noticeService.delete(id);
	}

	@ApiOperation(value = "置顶")
	@RequestMapping(value = "/toTop", method = RequestMethod.PUT)
	public ResultVO toTop(String id,String sessionId) {
		AssertUtil.notBlank(id, "id is required.");
		AssertUtil.notBlank(sessionId, "sessionId is required.");
		return noticeService.toTop(id,sessionId);
	}

	@ApiOperation(value = "改变状态")
	@RequestMapping(value = "/change/status", method = RequestMethod.PUT)
	public ResultVO changeStatus(String id) {
		AssertUtil.notBlank(id, "id is required.");
		return noticeService.changeStatus(id);
	}
	
	@ApiOperation(value = "使失效")
	@RequestMapping(value = "/failure", method = RequestMethod.PUT)
	public ResultVO makeFailure(String id) {
		AssertUtil.notBlank(id, "id is required.");
		return noticeService.makeFailure(id);
	}
	
	@ApiOperation(value = "获取所有未读公告列表")
	@RequestMapping(value = "/unread",method = RequestMethod.GET)
	public Map<String, Object> findAllUnread(String sessionId, DataTablesParamVO dtp) {
		AssertUtil.notBlank(sessionId, "sessionId is required.");
		return noticeService.findAllUnread(sessionId, dtp);
	}
	
	@ApiOperation(value = "确认消息记录")
	@RequestMapping(value = "/confirm",method = RequestMethod.PUT)
	public ResultVO confirmNotice(NoticeReadVO read){
		AssertUtil.notBlank(read.getSessionId(), "sessionId is required.");
		AssertUtil.notNull(read.getCurrentRecord(), "currentRecord is required.");
		return noticeService.confirmNotice(read);
	}
	
	@ApiOperation(value = "确认所有消息记录")
	@RequestMapping(value = "/confirm/all",method = RequestMethod.PUT)
	public ResultVO confirmAllNotice(String sessionId) {
		AssertUtil.notBlank(sessionId, "sessionId is required.");
		return noticeService.confirmAllNotice(sessionId);
	}
}
